frappe.listview_settings['Job Card'] = {
    onload: function (listview) {
        frappe.db.get_single_value("Light Mes Settings", "enable_job_card_multi_tracking_number")
        .then(r =>{
            if (r && r===1){
                var df = {
                        doctype:"Tracking Detail",
                        fieldname: "tracking_number",
                        label:"Tracking Number",
                        fieldtype: "Link",
                        options:"Tracking Number",
                        onchange: function(){
                            listview.start = 0;
                            listview.refresh();
                            listview.on_filter_change();
                        },
                }
                listview.page.add_field(df, listview.filter_area.standard_filters_wrapper);
            }
        });

        listview.page.add_action_item(__('Update Prices'), function() {
            const selected_docs = listview.get_checked_items();
            if (selected_docs.length === 0) {
                frappe.msgprint({
                    title: __('Message'),
                    indicator: 'red',
                    message: __('Please select at least one Job Card')
                });
                return;
            }
            
            frappe.confirm(
                __('Are you sure you want to update prices for selected Job Cards?'),
                function() {
                    frappe.call({
                        method: 'light_mes.light_mes.overrides.job_card.bulk_update_operation_price',
                        args: {
                            names: selected_docs.map(d => d.name).join(',')
                        },
                        freeze: true,
                        freeze_message: __('Updating prices...'),
                        callback: function(r) {
                            listview.refresh();
                        }
                    });
                }
            );
        });
    }
};